package com.xhsj.user.pms.patent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xhsj.user.pms.base.BaseController;
import com.xhsj.user.pms.patent.domain.Bill;
import com.xhsj.user.pms.patent.domain.BillList;
import com.xhsj.user.pms.patent.service.BillService;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 苏小林
 * @Description 缴费通知单表
 * @createTime 2020年02月20日 10:39:00
 */
@Slf4j
@Api(value = "缴费通知单表", description = "缴费通知单表")
@RequestMapping("/bill")
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class BillController extends BaseController {

    @Autowired
    private BillService billService;

    /**
     * 保存缴费通知
     */
    @RequestMapping(value = "/saveBill", method = RequestMethod.POST)
    @ApiOperation(value = "保存缴费通知", notes = "保存缴费通知")
    public Message saveBill(@RequestBody Bill bill) {
        try {

            //查找缴费通知是否已经缴费了
//           List<Map<String,Object>> billByDisclosureId= billService.getBillByDisclosureId(bill.getBillForDisclosure());
//           if(billByDisclosureId.size() == 0){
//               Map<String,Object> param = new HashMap<>();
//                param.put("statusId", StatusConstant.X);
//                param.put("disclosureId",bill.getBillForDisclosure());
//               //修改交底书状态
//               Message message2 = workflowExecService.updateOpinionStatus(param);
//               if(!("0".equals(String.valueOf(message2.get("code"))))){
//                   TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键
//                   return Message.error(1, "修改交底书状态异常");
//                   }
//               //删除流程指派人
//               int i3 = workflowIdentityLinkMapper.deleteByDisclosureId(bill.getBillForDisclosure());
//               if (i3 <= 0) {
//                   log.error("设置下一个节点流程指定人异常,{}",i3);
//                   TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键
//                   return Message.error(1, "工单异常请联系管理员");
//               }
//           }
            bill.setBillType(1);
            int i = billService.saveBill(bill);
           if(i != 1){
               log.error("保存缴费失败,{}",i);
//               TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键
               return Message.error(1, "保存缴费失败");
           }
            return Message.success("创建成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error("创建失败!");
        }
    }

    /**
     * 查询缴费列表
     */
    @RequestMapping(value = "/findBillList", method = RequestMethod.POST)
    @ApiOperation(value = "查询缴费列表", notes = "保存缴费通知")
    public Message findBillList(@RequestBody Map<String, Object> data) {
        try {

            if (Objects.isNull(data.get("type"))) {
                return Message.error(1,"type不能为空");
            }
            IPage<BillList> list = billService.findBillList(data);
            return Message.success("成功",list.getRecords(),list.getTotal(),list.getPages());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return Message.error(1,e.getMessage());
        }
    }

    /**
     * 查询专利缴费详情
     */
    @RequestMapping(value = "/billDetailByDisclosure", method = RequestMethod.GET)
    @ApiOperation(value = "查询专利缴费详情", notes = "查询专利缴费详情")
    public Message billDetailByDisclosure(@RequestParam("disclosureId") String disclosureId){
        try {
            List<BillList> list = billService.billDetailByDisclosure(disclosureId);
            if (list.size() == 0) {
                return Message.error(1,"没有记录存在");
            }
            List<Object> arrayList = Lists.newArrayList();
            for (BillList billList : list) {
                Map<String, Object> listMap = Maps.newHashMap();
                listMap.put("name","缴费发票");
                listMap.put("date",billList.getEnewTime()+"-----"+billList.getExpireTime());
                arrayList.add(listMap);
            }

            Map<String, Object> map = Maps.newHashMap();
            map.put("num",list.get(0).getNum());
            map.put("inventor",list.get(0).getInventor());
            map.put("enewTime",list.get(0).getEnewTime());
            map.put("expireTime",list.get(0).getExpireTime());
            map.put("list",arrayList);
            return Message.success("成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error(1,e.getMessage());
        }
    }


    /**
     * 批量删除
     * @author 苏小林
     * @date 2020/2/21 15:55
     * @param: list
     */
    @ApiImplicitParam(name = "list", value = "需要删除的id集合:[\"id1\",\"id2\"...]", required = true)
    @ApiOperation(value = "批量删除", notes = "批量删除。")
    @PostMapping("/delByIds")
    public Message delByIds(@RequestBody List<String> list){
        try {
            billService.delByIds(list);
            return Message.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error(1,"失败");
        }
    }

}
